package com.kitty.game.function.service;

import com.kitty.game.chengzhangmiji.message.ChengZhangMiJiShow;
import com.kitty.game.role.model.Role;
import com.kitty.game.enter.RespGeneralNotify;
import com.kitty.game.function.FunctionDataPool;
import com.kitty.game.function.model.FunctionType;
import com.kitty.game.utils.BitSet;
import com.kitty.game.utils.NotifyModules;
import com.kitty.listener.EventType;
import com.kitty.listener.annotation.EventHandler;
import com.kitty.listener.event.CreateRoleEvent;
import com.kitty.listener.event.LoginEvent;
import com.kitty.listener.event.RoleLevelUpEvent;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Service;

//后加


/**
 * 处理玩家开启功能的业务类
 */
@Service
public class FunctionService {

    /**
     * 处理玩家登录事件
     */
    @EventHandler(EventType.LOGIN)
    public void handleLoginEvent(LoginEvent loginEvent) {
        Role role = loginEvent.getRole();
        handleRoleLevelUpEvent(new RoleLevelUpEvent(EventType.ROLE_LEVEL_UP, role, role.getLevel(), role.getLevel()));
       pushFunctions(role);
    }

    /**
     * 发送已开放功能到客户端
     */
    private void pushFunctions(Role role) {
        BitSet otherIconPara = BitSet.NEW();//第一个位置
        BitSet mainIconPara = BitSet.NEW();//第二个位置
        BitSet mainIconParaEx = BitSet.NEW();//第三个位置
        BitSet mainIconParaEx1 = BitSet.NEW();//第四个位置

        for (FunctionType functionType : FunctionType.values()) {
            /**开启等级等于0的，表示默认开启的*/
            if (functionType.getOpenLevel() != 0) {
                continue;
            }
            openFunction(role, functionType);
        }
        role.getFuncs().getIcons().forEach(mainIconPara::setBit);
        role.getFuncs().getTabIcons().forEach(otherIconPara::setBit);
        role.getFuncs().getIcons2().forEach(mainIconParaEx::setBit);

        String iconHex = toHexString(otherIconPara.getI32(), mainIconPara.getI32(), mainIconParaEx.getI32(), mainIconParaEx1.getI32());
        // iconHex = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"; //000ADFF8071FFFFF0000000000000000   007ADFF8271FFFFF  00014910  00000000
        // 007ADFF8271FFFFF      0001091000000000  00000B0000000000
//        if (role.getLevel() >= 80) {
//            iconHex = iconHex.substring(0, 16) + "0001601000000000";
//        } else if (role.getLevel() >= 70 && role.getLevel() < 80) {
//            iconHex = iconHex.substring(0, 16) + "0000601000000000";
//        } else {
//            iconHex = iconHex.substring(0, 16) + "0001111000000000";
//        }
        if (role.getLevel() >= 80) {
            iconHex = iconHex.substring(0, 16) + "ff0fffffff1f60f0";
        } else if (role.getLevel() >= 70 && role.getLevel() < 80) {
            iconHex = iconHex.substring(0, 16) + "ff0fffffff1f60f0";
        } else {
            iconHex = iconHex.substring(0, 16) + "ff0fffffff1f60f0";
        }



        //007ADFF827DFFFFF   0001601000000000
        //iconHex = iconHex.substring(0, 16) + "0000001000000000";
        //007ADFF8271FFFFF                          00000B0000000000
        RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
        respGeneralNotify.setNotify((short) NotifyModules.NOTICE_UPDATE_MAIN_ICON);
        respGeneralNotify.setValue(iconHex);
        MessagePusher.pushMessage(role, respGeneralNotify);


        ChengZhangMiJiShow chengZhangMiJiShow  = new ChengZhangMiJiShow();
        chengZhangMiJiShow.setStart_time((int)(System.currentTimeMillis()/1000-3600*24));
        chengZhangMiJiShow.setEnd_time((int)(System.currentTimeMillis()/1000+3600*24*7));
        chengZhangMiJiShow.setPlay_guide((byte)0);
        MessagePusher.pushMessage(role, chengZhangMiJiShow);
        //PromoteMgr:clickPromote(tag)
      //  Resexelua resexelua = new Resexelua();
       //  String map_s = "PromoteMgr:clickPromote(17)";//true
      //  resexelua.setMsg(map_s);
       //  MessagePusher.pushMessage(role, resexelua);
    }


    /**
     * 取图标信息HEX
     */
    private String toHexString(int... args) {
        StringBuilder result = new StringBuilder();
        for (int i : args) {
            StringBuilder inputBuilder = new StringBuilder(Integer.toHexString(i));
            while (inputBuilder.length() < 8) {
                inputBuilder.insert(0, "0");
            }
            result.append(inputBuilder.toString());
        }
        return result.toString().toUpperCase();
    }

    /**
     * 处理角色升级事件
     */
    @EventHandler(EventType.ROLE_LEVEL_UP)
    public void handleRoleLevelUpEvent(RoleLevelUpEvent roleLevelUpEvent) {
        /**升级到指定等级开启功能*/
        Role role = roleLevelUpEvent.getRole();
        if (role == null) {
            return;
        }
        /**遍历所有按等级开启的功能，达到等级时开启*/
        for (FunctionType functionType : FunctionDataPool.upgradeFunctions.values()) {
            /**开启等级需要大于升级前的角色等级，小于等级升级后的角色等级*/
            if (functionType.getOpenLevel() <= roleLevelUpEvent.getLevel()) {
                openFunction(role, functionType);
            }
        }
        if (role.getLevel() > 20) {
            pushFunctions(role);
        }
    }

    /**
     * 处理创建角色事件
     */
    @EventHandler(EventType.CREATE_ROLE)
    public void handleCreateRoleEvent(CreateRoleEvent createRoleEvent) {
        /**角色创建时添加默认开启的功能*/
        Role role = createRoleEvent.getRole();

        for (FunctionType functionType : FunctionType.values()) {
            /**开启等级等于0的，表示默认开启的*/
            if (functionType.getOpenLevel() != 0) {
                continue;
            }

            openFunction(role, functionType);
        }

    }

    /**
     * 开启功能
     */
    private void openFunction(Role role, FunctionType functionType) {
        role.getFuncs().openFunction(functionType.getFunctionId(), functionType.getIconType());
        role.save();
    }
}
